//
//  DetailOfUserTableViewCell.m
//  LoveOfPet
//
//  Created by 芦彬 on 15/11/18.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "DetailOfUserTableViewCell.h"
#import "LuBDefine.h"
#import "SDWebImageManager.h"
#import "UIImageView+WebCache.h"

@implementation DetailOfUserTableViewCell

- (void)dealloc{
    [_detailOfUser release];
    [_avatarImageView release];
    [_contentLabel release];
    [_disnameLabel release];
    [_louLabel release];
    [_nicknameLael release];
    [_timeLabel release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createDetailOfUserTableViewCell];
    }
    return self;
}

- (void)createDetailOfUserTableViewCell{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    //---评论---
    self.avatarImageView = [[UIImageView alloc]init];
    self.avatarImageView.backgroundColor = [UIColor redColor];
    [self addSubview:_avatarImageView];
    [_avatarImageView release];
    
    self.disnameLabel = [[UILabel alloc]init];
//    self.disnameLabel.backgroundColor = [UIColor orangeColor];
    [self addSubview:_disnameLabel];
    [_disnameLabel release];
    
    self.louLabel = [[UILabel alloc]init];
//    self.louLabel.backgroundColor =[UIColor purpleColor];
    [self addSubview:_louLabel];
    [_louLabel release];
    
    self.nicknameLael = [[UILabel alloc]init];
//    self.nicknameLael.backgroundColor = [UIColor blueColor];
    [self addSubview:_nicknameLael];
    [_nicknameLael release];
    
    self.timeLabel = [[UILabel alloc]init];
//    self.timeLabel.backgroundColor = [UIColor magentaColor];
    [self addSubview:_timeLabel];
    [_timeLabel release];
    
    self.contentLabel = [[UILabel alloc]init];
//    self.contentLabel.backgroundColor = [UIColor greenColor];
    [self addSubview:_contentLabel];
    [_contentLabel release];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.avatarImageView.frame = CGRectMake(10, 10, WIDTH / 8, WIDTH / 8);
    self.avatarImageView.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.avatarImageView.layer.shadowOffset = CGSizeMake(5, 5);
    self.avatarImageView.layer.borderColor = [[UIColor whiteColor] CGColor];
    self.avatarImageView.layer.shadowOpacity = 1;
    self.avatarImageView.layer.borderWidth = 3;
    
    self.disnameLabel.frame = CGRectMake(10 * 2 + self.avatarImageView.frame.size.width, 15, WIDTH /2, 30);
    self.disnameLabel.textColor = [UIColor colorWithRed:0.506 green:0.0 blue:0.0254 alpha:1.0];
    self.disnameLabel.font = [UIFont systemFontOfSize:15];

    self.timeLabel.frame = CGRectMake(WIDTH - WIDTH / 4 - 10, 15, WIDTH / 4, 30);
    self.timeLabel.textColor = [UIColor grayColor];
    self.timeLabel.textAlignment = NSTextAlignmentRight;
    self.timeLabel.font = [UIFont systemFontOfSize:12];
    
    self.louLabel.frame = CGRectMake(10, 15 + self.avatarImageView.frame.size.height, WIDTH / 8, 30);
    self.louLabel.font = [UIFont systemFontOfSize: 16];
    self.louLabel.textAlignment = NSTextAlignmentCenter;
    self.louLabel.textColor = [UIColor grayColor];
    
    self.contentLabel.frame = CGRectMake(10 * 2 + self.avatarImageView.frame.size.width, 20 + self.disnameLabel.frame.size.height + 10 ,WIDTH -  10 * 2 - self.avatarImageView.frame.size.width - 10, self.autoHeight);
    self.contentLabel.font = [UIFont systemFontOfSize: 12];
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.textColor = [UIColor grayColor];
}

+ (CGFloat)heightOfLabel:(NSString *)text andTextFont:(UIFont *)font withLabelWidths:(CGFloat)widths{
    CGSize size = CGSizeMake(widths, MAXFLOAT);
    NSDictionary *attributeDic = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributeDic context:nil];
    return frame.size.height;
}

//评论
- (void)setDetailOfUser:(DetailOfUser *)detailOfUser{
    if (_detailOfUser != detailOfUser) {
        [_detailOfUser release];
        _detailOfUser = [detailOfUser retain];
    }
    NSURL *avatarUrl = [NSURL URLWithString:detailOfUser.avatar];
    [self.avatarImageView sd_setImageWithURL:avatarUrl placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    
    self.disnameLabel.text = detailOfUser.disname;
    self.louLabel.text = detailOfUser.lou;
    self.nicknameLael.text = detailOfUser.nickname;
    self.contentLabel.text = detailOfUser.content;
    self.timeLabel.text = detailOfUser.time;
    
    _autoHeight = [DetailOfUserTableViewCell heightOfLabel:self.contentLabel.text andTextFont:[UIFont systemFontOfSize:12] withLabelWidths:WIDTH - 40];
}

+ (DetailOfUserTableViewCell *)detailOfUserTabelViewCellWithTableView:(UITableView *)tableView{
    static NSString *detailOfUserTableViewCell = @"detailOfUserTableViewCell";
    DetailOfUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:detailOfUserTableViewCell];
    if (!cell) {
        cell = [[[DetailOfUserTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:detailOfUserTableViewCell] autorelease];
    }
    return cell;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
